#include "MainWindow.hh"
#include <QtCore>
#include "NewGameWindow.hh"

using namespace Gui;
using namespace Network;

MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent)
{
	ui.setupUi(this);
    QListWidget* list = findChild< QListWidget* >("listWidget");
	connect(
	  list, SIGNAL(itemClicked(QListWidgetItem*)),
	  this, SLOT(belepes(QListWidgetItem*))
	);
}

void MainWindow::show_game_list(GameList::GameListInfo gameListInfo) {

    QListWidget* list = findChild< QListWidget* >("listWidget");

    for (
      GameList::GameListInfo::const_iterator gameInfo = gameListInfo.begin();
      gameInfo != gameListInfo.end();
      ++gameInfo
    ) {
        list->addItem(gameInfo.key());
    }
}

void MainWindow::new_game()
{
	NewGameWindow* newgamew = new NewGameWindow();
	newgamew->setWindowModality(Qt::WindowModal);
    connect(
      newgamew,      SIGNAL(send_new_game(QString, u2, u2)),
      this,			 SLOT(send_new_game(QString, u2, u2))
    );
	newgamew->show();
}

void MainWindow::send_new_game(QString name, u2 min, u2 max)
{

	emit send_new_game_message(name, min, max);
}

void MainWindow::refresh()
{
	emit refresh_game_list();
}

void MainWindow::send_chat_msg() {
	QLineEdit* chatNewTextbox = findChild< QLineEdit* >("chatNewTextbox");
	if (chatNewTextbox && chatNewTextbox->text().length() > 0)
	{
		emit send_chat_msg_to_server(chatNewTextbox->text());
		chatNewTextbox->clear();
	}
}

void MainWindow::belepes(QListWidgetItem* qi)
{
	emit join(qi->text());
}

void MainWindow::chat_msg_received(QString sender, QString msg) {
	QPlainTextEdit* chatNewTextbox = findChild< QPlainTextEdit* >("chatWindow");
	chatNewTextbox->appendPlainText(sender + QString(": ") + msg);
}

